﻿using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace MetroDictionary
{
    public class WebViewEx
    {
        public static readonly DependencyProperty StringSourceProperty =
            DependencyProperty.RegisterAttached("StringSource", typeof(string), typeof(WebViewEx),
                                                new PropertyMetadata("", OnStringSourceChanged));

        private static void OnStringSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var webView = (WebView) d;
            webView.LoadCompleted -= WebViewLoadCompleted;
            webView.LoadCompleted += WebViewLoadCompleted;
            webView.Visibility = Visibility.Collapsed;
            webView.NavigateToString((string) e.NewValue ?? "");
        }

        private static void WebViewLoadCompleted(object sender, NavigationEventArgs e)
        {
            var webView = (WebView) sender;
            webView.Visibility = Visibility.Visible;
        }

        public static string GetStringSource(DependencyObject obj)
        {
            return (string) obj.GetValue(StringSourceProperty);
        }

        public static void SetStringSource(DependencyObject obj, string value)
        {
            obj.SetValue(StringSourceProperty, value);
        }
    }
}